<?php
/**
 * 广告中心控制器
 * User:LLW
 * Date:2020-03-18
 */

namespace app\caidongweb\controller;

use app\caidongweb\model\AdverModel;
use think\Request;

class Adver extends BaseController
{
    //初始化程序
    public function __construct(Request $request = null)
    {
        parent::__construct();
        $this->adv = new AdverModel();
    }

    /******************************** 轮播列表相关 ***************************************/
    /**
     * 首页
     * @return \think\response\View
     */
    public function shuffList()
    {
        $list = $this->adv->getList();
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        return view();
    }

    /**
     * 添加编辑
     * @return \think\response\View
     */
    public function shuffAddEdit()
    {
        if(input('post.')){
            $res = $this->adv->getAddEdit();
            return $res;
        }else{
            $find = $this->adv->getFind();
            $this->assign('find',$find);
            return view();
        }
    }

    /**
     * 回收站
     */
    public function adverBin()
    {
        $list = $this->adv->getBin();
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        return view();
    }

    /**
     * 永久删除文章
     * @return array
     */
    public function bin_del()
    {
        $res = $this->del('adver',['ae_id'=>input('id')]);
        if($res){
            return $this->returnData(1,'成功');
        }
        return $this->returnData(-1,'失败');
    }
    /******************************** 文件上传 ***************************************/
    /**
     * 图片上传
     * @return array
     */
    public function uploads()
    {
        $file = \request()->file('file');
        $newname = time();
        $path = 'uploads/adver/';
        if(!file_exists($path)){
            mkdir($path,777);
        }
        $saveInfo = $file->validate(['size'=>1567800,'ext'=>'jpg,png,gif'])->move($path,$newname);
        if($saveInfo){
            $pathInfo = '/'.$path.$saveInfo->getSaveName();
            return ['code'=>1,'msg'=>'上传成功','src'=>$pathInfo];
        }else{
            return ['code'=>-1,'msg'=>'上传失败'];
        }
    }
}